//
//  CommonListTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/2.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class CommonListTableViewCell: UITableViewCell {
    @IBOutlet weak var xiajiaView: UIView!
    @IBOutlet weak var goodsName: UILabel!
    @IBOutlet weak var goodsImage: UIImageView!
    @IBOutlet weak var nowPrice: UILabel!
    @IBOutlet weak var bazaarPrice: UILabel!
    @IBOutlet weak var gouwuButton: UIButton!
    
    var model:SiteGoodsModel!
    
    var backClosure:TouchBlock!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.xiajiaView.backgroundColor = UIColor.init(hexColor: "#000000", alpha: 0.6)
        self.xiajiaView.corner(byRoundingCorners: [.topLeft,.topRight,.bottomLeft,.bottomRight], radii: 10)
        self.goodsImage.corner(byRoundingCorners:[.topLeft,.topRight,.bottomLeft,.bottomRight], radii: 10)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func reload(withModel model:SiteGoodsModel){
        self.model = model
        self.goodsName.text = model.name
        self.goodsImage.my_setImage(url: model.img, placeholder: Api.placeHolder.rawValue)
        self.nowPrice.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(model.price)/斤", changeString: "\(model.price)".getPriceInteger(), font: UIFont.systemFont(ofSize: 17))
        self.bazaarPrice.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(model.marketPrice)", deleString: "￥" + "\(model.marketPrice)")
        
        if model.state == 0 {
            self.normalStyle()
        }else{
            self.soldOutStyle()
        }
    }
    
    // MARK:添加购物车
    @IBAction func addButtonClick(_ sender: UIButton) {
        guard Utils.isLogined() else{
            if self.backClosure != nil {
                self.backClosure()
            }
            return
        }
        Utils.insertCart(goodsId: self.model.id) { (value) in
            if value.0 {
                PKhud.showText(text: "添加成功~")
            }else{
                PKhud.showText(text: value.1)
            }
        }
        
    }
    
    // MARK:下架的状态
    func soldOutStyle(){
        self.xiajiaView.isHidden = false
        self.gouwuButton.setBackgroundImage(UIImage.init(named: "gwc_bukexuan"), for: .normal)
        self.gouwuButton.isUserInteractionEnabled = false
    }
    
    
    // MARK:默认的状态
    func normalStyle(){
        self.xiajiaView.isHidden = true
        self.gouwuButton.setBackgroundImage(UIImage.init(named: "icon_gouwuche"), for: .normal)
        self.gouwuButton.isUserInteractionEnabled = true
    }
    
}
